x = int(input())
a = str(x)
_max = 0
b = 0
c = 0
d = 1
if x > 9999 or x < 1000:
    print('0')
else:
    for i in range(4):
        for _ in range(2, int(a[c:d]) - 1):
            b = 1
            break
        if b == 0 and _max < int(a[c:d]):
            _max = int(a[c:d])
        b = 0
        c += 1
        d += 1
    c = 0
    d = 2
    for i in range(3):
        for _ in range(2, int(a[c:d]) - 1):
            if int(a[c:d]) % _ == 0:
                b = 1
                break
        if b == 0 and _max < int(a[c:d]):
            _max = int(a[c:d])
        b = 0
        c += 1
        d += 1
    c = 0
    d = 3
    for i in range(2):
        for _ in range(2, int(a[c:d]) - 1):
            if int(a[c:d]) % _ == 0:
                b = 1
                break
            if b == 0 and _max < int(a[c:d]):
                _max = int(a[c:d])
        b = 0
        c += 1
        d += 1
    c = 0
    d = 4
    for _ in range(2, int(a[c:d]) - 1):
        if int(a[c:d]) % _ == 0:
            b = 1
            break
    if b == 0 and _max < int(a[c:d]):
        _max = int(a[c:d])
    if _max == 1:
        print('0')
    else:
        print(_max)